<template>
  <view>
    <view class="goods-list">
      <block v-for="(goods, i) in goodsList" :key="i">
        <view class="goods-item"  @click="goDetail(goods)">
          <!-- 商品左侧图片区域 -->
          <view class="goods-item-left">
            <image :src="goods.goods_small_logo || defaultpic" class="goods-pic"></image>
          </view>
          <!-- 商品右侧信息区域 -->
          <view class="goods-item-right">
            <!-- 商品标题 -->
            <view class="goods-name">{{goods.goods_name}}</view>
            <view class="goods-info-box">
              <!-- 商品价格 -->
              <view class="goods-price">￥{{goods.goods_price}}</view>
            </view>
          </view>
        </view>
      </block>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          query: '',
          cid: ',',
          pagenum: '',
          pagesize: 10,
        },
        goodsList: [],
        total: 0,
        defaultpic: 'https://img2.baidu.com/it/u=3724981271,746609293&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
        isloading: false
      }
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    methods: {
      async getGoodsList(cb) {
        this.isloading = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        this.isloading = false
        cb && cb()
        if (res.meta.status !== 200) return uni.$showMsg()
        // this.goodsList = res.message.goods
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
      },
      goDetail(goods) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        })
      }
    },
    onReachBottom() {
      if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total)
      return uni.$showMsg('数据加载完毕!')
      if(this.isloading) return
      this.queryObj.pagenum += 1
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.total = 0
      this.isloading = true
      this.goodsList = []
      this.getGoodsList(() => uni.stopPullDownRefresh())
    },
  }
</script>

<style lang="scss">
  page{ 
    background-color: #eee;
  }
  
  .goods-list {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
  }
  
  .goods-item {
    display: flex;
    flex-direction: column;
    margin: 6px;
    width: 310rpx;
    border-radius: 10px;
    background-color: #fff;
    padding: 5px;
      .goods-pic {
        width: 100%;
        height: 150px;
        display: block;
      }
    
    .goods-item-right {
      display: flex;
      height: 100%;
      flex-direction: column;
      justify-content: space-between;

      .goods-name {
        overflow: hidden;
        text-overflow: ellipsis;
        font-size: 11px;
      }
      
      .goods-info-box {
        display: flex;
        margin-top: 10px;
        justify-content: flex-start;
        align-items: flex-end;
      }

      .goods-price {
        font-size: 16px;
        color: #c00000;
      }
    }
  }
</style>
